package com.taiyi.renaissancemall.ui.main.search;

import android.content.Intent;
import android.util.Log;

import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.Utils.Utils;
import com.taiyi.renaissancemall.base.BasePresent;
import com.taiyi.renaissancemall.base.MyApp;
import com.taiyi.renaissancemall.bean.BaseResultBean;
import com.taiyi.renaissancemall.bean.EmptyBean;
import com.taiyi.renaissancemall.bean.SearchHistoryBean;
import com.taiyi.renaissancemall.bean.SearchKindBean;
import com.taiyi.renaissancemall.bean.SearchListBean;
import com.taiyi.renaissancemall.net.BaseUrl;
import com.taiyi.renaissancemall.net.HttpUtils;
import com.taiyi.renaissancemall.net.callbck.JsonCallback;
import com.taiyi.renaissancemall.ui.login.LoginActivity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Response;

public class SearchPresent extends BasePresent<SearchView> {

    public void getSearchHistory() {
        Map<String, String> map = new HashMap<>();
        HttpUtils.postRequest(BaseUrl.SEARCH_HISTORY, this, map, new JsonCallback<BaseResultBean<SearchHistoryBean>>() {
            @Override
            public BaseResultBean<SearchHistoryBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<SearchHistoryBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.searchHistorySuccess(response.body().data);
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.searchHistoryFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<SearchHistoryBean>> response) {
                super.onError(response);
            }
        });
    }


    public void getSearchList() {
        Map<String, String> map = new HashMap<>();
        map.put("pagenum", view.getPageNo());
        map.put("categoryid", view.getCategoryId());
        map.put("pricesort", view.getPriceValue());
        map.put("salesort", view.getSaleValue());
        map.put("bvsort", view.getBvValue());
        map.put("beginprice", view.getStartPrice());
        map.put("endprice", view.getEndPrice());
        map.put("content", view.getSearchContent());
        Log.i("dsdasdasda", "----" + map.toString());

        HttpUtils.postRequest(BaseUrl.SEARCH_LIST, this, map, new JsonCallback<BaseResultBean<SearchListBean>>() {
            @Override
            public BaseResultBean<SearchListBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<SearchListBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    Log.i("dsssssssss", "----" + response.body().data.toString());
                    view.getSearchListSuccess(response.body().data);
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.getSearchListFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<SearchListBean>> response) {
                super.onError(response);
            }
        });
    }

    public void getKindList() {
        Map<String, String> map = new HashMap<>();
        HttpUtils.postRequest(BaseUrl.SEARCH_KIND, this, map, new JsonCallback<BaseResultBean<List<SearchKindBean>>>() {
            @Override
            public BaseResultBean<List<SearchKindBean>> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<List<SearchKindBean>>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.getKindListSuccess(response.body().data);
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {

                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<List<SearchKindBean>>> response) {
                super.onError(response);
            }
        });
    }

    public void delHistoryList() {
        Map<String, String> map = new HashMap<>();
        HttpUtils.postRequest(BaseUrl.DEL_SEARCH, this, map, new JsonCallback<BaseResultBean<EmptyBean>>() {
            @Override
            public BaseResultBean<EmptyBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<EmptyBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    ToastUtils.showShortToast(response.body().msg_cn);
                    view.delHistoryListSuccess();
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    ToastUtils.showShortToast(response.body().msg_cn);
                    view.delHistoryListFailure();
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<EmptyBean>> response) {
                super.onError(response);
            }
        });
    }

}
